﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.DataAccess;



namespace WarehouseManagement.BusinessLogic.BC
{
    public class CurrencyBC : BaseBC
    {

        public CurrencyBC()
        {
            //_repo = new CurrencyRepository();
        }


        public IEnumerable<CurrencyDTO> GetCurrencyList()
        {
            return Context.Currencies.AsEnumerable()
                .Select(e => ConvertCurrency(e))
                .ToList();
        }

        public CurrencyDTO GetCurrencyById(int id)
        {
            var item = Context.Currencies.SingleOrDefault(e => e.Id == id);
            if (item == null) return null;
            return ConvertCurrency(item);
        }

        public CurrencyDTO GetCurrencyByCode(string code)
        {
            var item = Context.Currencies.SingleOrDefault(e => e.CurrencyCode == code);
            if (item == null) return null;
            return ConvertCurrency(item);
        }

        public void DeleteCurrency(int currencyId)
        {
            var entity = GetEntity(currencyId);
            if (entity != null)
            {
                Context.Currencies.DeleteOnSubmit(entity);
                SubmitChanges();
            }
        }
        public void AddCurrency(CurrencyDTO dto)
        {
            var entity = ConvertCurrency(dto);
            Context.Currencies.InsertOnSubmit(entity);
            SubmitChanges();
        }

        public void UpdateCurrency(CurrencyDTO dto)
        {
            var entity = GetEntity(dto.CurrencyId);
            entity.CurrencyCode = dto.CurrencyCode;
            entity.CurrencyName = dto.CurrencyName;
            entity.ExchangeRate = dto.ExchangeRateVND;
            SubmitChanges();
        }
        private Currency GetEntity(int id)
        {
            return Context.Currencies.SingleOrDefault(e => e.Id == id);
        }
        //public CurrencyExchangeRateDTO GetExchangeRateOf(int currencyId, DateTime fromDate)
        //{
        //    var list = Context.CurrencyExchangeRates
        //        .Where(e => e.CurrencyId == currencyId && e.Date.Date <= fromDate.Date)
        //        .OrderByDescending(e => e.Date)
        //        .Select(e => ConvertCurrency(e));
        //    if (list.Count() > 0)
        //    {

        //        var firstItem = list.FirstOrDefault();
        //        if (firstItem != null)
        //            return firstItem;
        //    }
        //    return new CurrencyExchangeRateDTO() { CurrencyId = currencyId, ExchangeRateInVND = 0 };

        //}

        private Currency ConvertCurrency(CurrencyDTO dto)
        {
            return new Currency()
            {
                Id = dto.CurrencyId,
                CurrencyCode = dto.CurrencyCode,
                CurrencyName = dto.CurrencyName,
                ExchangeRate = dto.ExchangeRateVND
            };
        }

        private CurrencyDTO ConvertCurrency(Currency entity)
        {
            return new CurrencyDTO()
            {
                CurrencyId = entity.Id,
                CurrencyCode = entity.CurrencyCode,
                CurrencyName = entity.CurrencyName,
                ExchangeRateVND = entity.ExchangeRate
            };
        }

        //private CurrencyExchangeRateDTO ConvertCurrency(CurrencyExchangeRate entity)
        //{
        //    return new CurrencyExchangeRateDTO()
        //    {
        //        CurrencyId = entity.CurrencyId,
        //        Date = entity.Date,
        //        ExchangeRateInVND = entity.ExchangeRateInVND,
        //        Id = entity.Id
        //    };
        //}

        //private CurrencyExchangeRate ConvertCurrency(CurrencyExchangeRateDTO dto)
        //{
        //    return new CurrencyExchangeRate()
        //    {
        //        CurrencyId = dto.CurrencyId,
        //        Date = dto.Date,
        //        ExchangeRateInVND = dto.ExchangeRateInVND,
        //        Id = dto.Id
        //    };
        //}
    }
}
